package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.UseInventoryKnifeDao;
import com.wing.cabinet.model.entity.UseInventoryKnife;
import com.wing.cabinet.model.request.UseInventoryKnifeForm;
import com.wing.cabinet.model.response.UseInventoryKnifeExportVO;
import com.wing.cabinet.model.response.UseInventoryKnifeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 刀具在用库存-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class UseInventoryKnifeServiceImpl extends ServiceImpl<UseInventoryKnifeDao, UseInventoryKnife> implements UseInventoryKnifeService {

    @Override
    public int add(UseInventoryKnifeForm useInventoryKnifeForm) {

        //校验

        UseInventoryKnife useInventoryKnife =new UseInventoryKnife();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(UseInventoryKnife.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(useInventoryKnifeForm, useInventoryKnife, copyOptions);

        return baseMapper.insert(useInventoryKnife);
    }

    @Override
    public List<UseInventoryKnifeVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<UseInventoryKnifeVO> pageList4app(Page page, Map<String, Object> params) {
        //List<UseInventoryKnifeVO> list = baseMapper.list4app(page, params);
        //if (params.get("labelCode")!=null&&!"".equals(params.get("labelCode").toString())&&list.size()>0){
        //    list.get(0).setQuantity(1);
        //}
        //page.setRecords(list);
        List<UseInventoryKnifeVO> list = baseMapper.listPage4app(page, params);
        for(UseInventoryKnifeVO useInventoryKnifeVO :list){
            List<UseInventoryKnifeVO> detailList = baseMapper.list4appDetail(useInventoryKnifeVO.getParentLabelCode());
            if(null != detailList && detailList.size() >0) {
                BeanUtils.copyProperties(detailList.get(0),useInventoryKnifeVO);
                useInventoryKnifeVO.setDetailList(detailList);
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public UseInventoryKnifeVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<UseInventoryKnifeExportVO> exportList(String enterpriseCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("enterpriseCode", enterpriseCode);
        List<UseInventoryKnifeExportVO> list = baseMapper.getUseInventoryKnifeExportList(params);
        return list;
    }
}
